﻿using Microsoft.Extensions.DependencyInjection;
using System;

namespace Fast
{
    /// <summary>
    /// 默认应用
    /// </summary>
    internal class DefaultFastApplication : FastApplicationBase, IFastApplication
    {
        public DefaultFastApplication(IServiceCollection services)
            : base(services)
        {
            services.AddSingleton<IFastApplication>(this);
        }

        /// <summary>
        /// 初始化 <see cref="Startup.Configure"/>
        /// </summary>
        /// <param name="serviceProvider"></param>
        public void Initialize(IServiceProvider serviceProvider)
        {
            Check.NotNull(serviceProvider, nameof(serviceProvider));

            SetServiceProvider(serviceProvider);

            InitializeStartups();
        }

        public override void Dispose()
        {
            base.Dispose();

            if (ServiceProvider is IDisposable disposableServiceProvider)
            {
                disposableServiceProvider.Dispose();
            }
        }
    }
}
